package net.monare.mifss.admin.ui.wiz.cards;

import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.ApiKeyUiModule;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.evn.CreateNewStorageListener;

import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.TextArea;

public class StorageFinalizeCard extends BaseWizardCard implements CardValidation{

	private static final long serialVersionUID = 6233219017903776604L;
	
	private TextArea messageResult;
	private NativeButton createStorage;
	private ApiKeyUiModule apiKeyUiModule;
	
	public StorageFinalizeCard(StorageCard storageCard, StoragePoolCard storagePoolCard) {
		
		super(MifssMessages.getMessage("title.add.storage.wizard.card.3"), "FINALIZATION_CARD");
		
		Label spacer1 = new Label("");
		Label spacer2 = new Label("");
		
		apiKeyUiModule = new ApiKeyUiModule();
		
		CreateNewStorageListener cnl = new CreateNewStorageListener(storagePoolCard, storageCard, this);
		
		String apiCreateButtonLabel = MifssMessages.getMessage("button.storage.create");
		String apiMessagesLabel = MifssMessages.getMessage("label.api.messages");
		
		createStorage = new NativeButton(apiCreateButtonLabel);
		createStorage.addClickListener(cnl);
		messageResult = new TextArea(apiMessagesLabel);
		messageResult.setWidth("480px");
        messageResult.setHeight("170px");

		
		apiKeyUiModule.addFormElement(spacer1);
		apiKeyUiModule.addFormElement(createStorage);
		apiKeyUiModule.addFormElement(spacer2);
		apiKeyUiModule.addFormElement(messageResult);
		
		addFormComponent(apiKeyUiModule);
	}

    public void lockCreateStorageButton(boolean locked){
        createStorage.setEnabled(locked);
    }

	public void writeMessageLine(String msg){
		
		String currText = messageResult.getValue();
		
		if(!currText.equals("")){
			currText = currText+ "\n";
		}
		
		currText = currText + msg;
		
		messageResult.setValue(currText);
		
	}
	
	public void clearMessages(){
		messageResult.setValue("");
	}
	
	public ApiKey getApiKey(){
		return apiKeyUiModule.getApiKey();
	}

	public boolean isCardValid() {
		if(getApiKey() != null){
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void validateCard() {
		// TODO Auto-generated method stub
		
	} 
	
	
}
